<template>
    <view>
        <!-- pages/taskSettle/taskSettle.wxml -->
        <view class="container">
            <view class="tabs">
                <view class="search" @tap="goSearch">
                    <view class="iconfont icon-date"></view>
                    <input type="text" placeholder="开始时间" disabled :value="sdt" />
                    <text>至</text>
                    <input type="text" placeholder="结束时间" disabled :value="edt" />
                </view>
            </view>
            <view class="bar">
                <view class="line">
                    <label>未结算订单：</label>
                    <text class="color-primary">{{ finishNum }}</text>
                    单
                </view>
                <view class="line">
                    <label>结算的金额：</label>
                    <input v-model="money" type="text" placeholder="请输入" />
                    元
                </view>
            </view>
            <!-- 列表内容 -->
            <block v-if="list.length">
                <view class="lists">
                    <view class="item" v-for="(item, index) in list" :key="index">
                        <view
                            :class="
                                item.status == 1
                                    ? 'tag blue2'
                                    : item.status == 2
                                    ? 'tag blue2'
                                    : item.status == 3
                                    ? 'tag green'
                                    : item.status == 4
                                    ? 'tag gray'
                                    : item.status == 5
                                    ? 'tag yellow'
                                    : item.status == 6
                                    ? 'tag blue'
                                    : 'tag red'
                            "
                        >
                            {{
                                item.status == 1
                                    ? '已接单'
                                    : item.status == 2
                                    ? '已开始'
                                    : item.status == 3
                                    ? '已完成'
                                    : item.status == 4
                                    ? '已取消'
                                    : item.status == 5
                                    ? '被驳回'
                                    : item.status == 6
                                    ? '已结算'
                                    : '待接单'
                            }}
                        </view>

                        <view class="info">
                            <label>订单编号：</label>
                            <text>{{ item.orderNo }}</text>
                        </view>

                        <view class="info">
                            <label>门店名称：</label>
                            <text>{{ item.storeName }}</text>
                        </view>

                        <view class="info">
                            <label>房间名称：</label>
                            <text>{{ item.roomName }}</text>
                        </view>

                        <view class="info">
                            <label>开始时间：</label>
                            <text v-if="item.startTime">{{ item.startTime }}</text>
                        </view>

                        <view class="info">
                            <label>完成时间：</label>
                            <text v-if="item.finishTime">{{ item.finishTime }}</text>
                        </view>

                        <view class="btns">
                            <button
                                hover-class="button-click"
                                class="btn bg-primary"
                                v-if="item.status == 3 || item.status == 5 || item.status == 6"
                                @tap="goTaskDetail"
                                :data-info="item.clearId"
                            >
                                查看详情
                            </button>
                        </view>
                    </view>
                </view>
                <view class="noteMore">下拉加载更多...</view>
            </block>
            <block v-else>
                <view class="nodata">暂无数据</view>
            </block>
            <view class="btn bg-primary" @tap="toSettle">一键结算</view>
        </view>
        <!-- 日期选择弹窗 -->
        <van-calendar :show="show" type="range" @close="onClose" @confirm="onConfirm" :show-confirm="false" allow-same-day :min-date="minDate" />
    </view>
</template>

<script>
// pages/taskSettle/taskSettle.js
const app = getApp();
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
var Moment = require('../../../lib/moment.js');
export default {
    data() {
        return {
            option2: [
                {
                    text: '全部状态',
                    value: ''
                },
                // { text: '待接单', value: 0 },
                {
                    text: '已接单',
                    value: 1
                },
                {
                    text: '已开始',
                    value: 2
                },
                {
                    text: '已完成',
                    value: 3
                },
                // { text: '已取消', value: 4 },
                {
                    text: '被驳回',
                    value: 5
                },
                {
                    text: '已结算',
                    value: 6
                }
            ],
            finishNum: 0,
            //未结算数量
            status: 3,
            sdt: '',
            edt: '',
            show: false,
            //日期控件
            minDate: new Date(2023, 0, 1).getTime(),
            info: '',
            //保洁员列表传过来的信息
            money: '',
            storeId: '',
            //列表搜索门店id
            stores: [],
            pageNo: 1,
            pageSize: 10,
            canLoadMore: true,
            //是否还能加载更多
            list: []
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        let info = JSON.parse(options.info);
        this.setData({
            info: info
        });
        this.getXiaLaListAdmin();
        this.getMainListdata('refresh');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        this.setData({
            pageNo: 1,
            canLoadMore: true,
            list: []
        });
        this.getMainListdata('refresh');
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getMainListdata('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        goSearch() {
            this.setData({
                show: true
            });
        },

        onClose() {
            this.setData({
                show: false
            });
        },

        formatDate(date) {
            date = new Date(date);
            return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
        },

        onConfirm(event) {
            const [start, end] = event.detail;
            this.setData({
                show: false,
                // sdt: this.formatDate(start),
                // edt: this.formatDate(end),
                sdt: Moment(start).format('YYYY-MM-DD'),
                edt: Moment(end).format('YYYY-MM-DD')
            });
            this.getMainListdata('refresh');
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            stores.unshift({
                                text: '全部门店',
                                value: ''
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //获取列表数据
        getMainListdata: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/manager/getClearManagerPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        storeId: that.info.storeId,
                        userId: that.info.userId,
                        status: that.status,
                        startTime: that.sdt,
                        endTime: that.edt
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            if (e == 'refresh') {
                                that.setData({
                                    list: info.data.list,
                                    finishNum: info.data.total
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.list;
                                let arrs = arr.concat(info.data.list);
                                that.setData({
                                    list: arrs,
                                    finishNum: info.data.total
                                });
                            }
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 结算
        toSettle() {
            if (this.money === '') {
                uni.showToast({
                    title: '请填写结算金额',
                    icon: 'error'
                });
                return;
            }
            let that = this;
            uni.showModal({
                title: '提示',
                content: '是否确定结算？',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/manager/settlementClearUser',
                                '1',
                                'post',
                                {
                                    userId: that.info.userId,
                                    storeId: that.info.storeId,
                                    money: Number(that.money),
                                    startTime: that.sdt,
                                    endTime: that.edt
                                },
                                app.globalData.userDatatoken.accessToken,
                                '保存中',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '结算成功'
                                        });
                                        setTimeout(() => {
                                            uni.navigateBack();
                                        }, 1000);
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it) => {
                if (it.value === event.detail) {
                    this.setData({
                        storeId: it.value
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        //状态下拉菜单发生变化
        statusDropdown(event) {
            this.option2.map((it) => {
                if (it.value === event.detail) {
                    this.setData({
                        status: it.value
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        // 详情页
        goTaskDetail(e) {
            var id = e.currentTarget.dataset.info;
            uni.navigateTo({
                url: '../taskDetail/taskDetail?id=' + id
            });
        }
    }
};
</script>
<style>
@import './taskSettle.css';
</style>
